/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.yaml;

import java.util.HashMap;
import java.util.Map;
import test.org.zoundry.gaedt.core.ZGaeCoreTestCase;


/**
 * Tests the YAML model class.
 */
public class ZGaeYAMLModelTest extends ZGaeCoreTestCase
{
    private ZGaeYAMLModel mYAMLModel;

    /**
     * @see junit.framework.TestCase#setUp()
     */
    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        mYAMLModel = new ZGaeYAMLModel();
        Map<String, Object> yamlData = mYAMLModel.getYamlData();
        yamlData.put("hello", "world");
        yamlData.put("foo", "bar");
        yamlData.put("average", 889L);

        // Sub-section of the YAML
        HashMap moreYaml = new HashMap();
        yamlData.put("moreYaml", moreYaml);
        moreYaml.put("child1", 10);
        moreYaml.put("child2", 15);
    }

    /**
     * @see junit.framework.TestCase#tearDown()
     */
    @Override
    protected void tearDown() throws Exception
    {
        mYAMLModel = null;
        super.tearDown();
    }

    /**
     * Simple test of the YAML query capabilities.
     */
    public void testQuerySimple()
    {
        Object actual = mYAMLModel.query("/missing/path");
        assertNull(actual);
        actual = mYAMLModel.query("/hello");
        assertEquals("world", actual);
        actual = mYAMLModel.query("/foo");
        assertEquals("bar", actual);
        actual = mYAMLModel.query("/average");
        assertEquals(889L, actual);

        actual = mYAMLModel.query("/moreYaml/child1");
        assertEquals(10, actual);
        actual = mYAMLModel.query("/moreYaml/child2");
        assertEquals(15, actual);
    }

    private static final String TEST_READ_WRITE_IN_DATA = "application: guestbook\n" +
    "version: 1\n" +
    "runtime: python\n" +
    "api_version: 1\n" +
    "\n" +
    "handlers:\n" +
    "- url: .*\n" +
    "  script: guestbook.py\n" +
    "- url: /index/test\n" +
    "  script: test.py";
    private static final String TEST_READ_WRITE_EXPECTED = "api_version: 1\n" +
    "application: guestbook\n" +
    "runtime: python\n" +
    "version: 1\n" +
    "handlers: \n" +
    "- script: guestbook.py\n" +
    "  url: .*\n" +
    "- script: test.py\n" +
    "  url: /index/test\n" +
    "";
    public void testReadWrite() throws Exception
    {
        ZGaeYAMLModel model = new ZGaeYAMLModel(TEST_READ_WRITE_IN_DATA);
        String actual = model.serialize();
        String expected = TEST_READ_WRITE_EXPECTED;
        assertEquals(expected, actual);
    }
}
